package com.tec.security.controller;

import com.tec.security.utils.mail.Email;
import com.tec.security.utils.mail.MailService;
import com.tec.security.utils.response.Result;
import com.tec.security.utils.security.service.UserInformationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.mail.MessagingException;
import java.util.concurrent.CompletableFuture;

@RestController
@Api(tags = "邮件")
@RequestMapping("/Mail")
public class EmailController {
    private final MailService mailService;
    private final UserInformationService userInformationService;
    @Autowired
    public EmailController(MailService mailService, UserInformationService userInformationService) {
        this.mailService = mailService;
        this.userInformationService = userInformationService;
    }

    @ApiOperation(value = "发送验证码邮件")
    @PostMapping("/SignupMail")
    public CompletableFuture<Result> mail(String address) throws MessagingException {
            Email email=new Email();
            email.setTos(new String[]{address});
            return mailService.commonEmail(email);
    }

    @ApiOperation(value="重设密码验证码")
    @PostMapping("/ResetMail")
    public CompletableFuture<Result> getMail(String address) throws MessagingException {
        if(userInformationService.selectEmail(address)){
            Email email=new Email();
            email.setTos(new String[]{address});
            return mailService.commonEmail(email);
        }else{
            return mailService.errorMail();
        }
    }
}
